﻿using Frame;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VideoFileOrganization.DocumentCleanup
{
    public partial class DocumentCleanupFrm : BaseFrm
    {
        public DocumentCleanupFrm()
        {
            InitializeComponent();
            this.Text = "文件清理";
            this.Index = 1;
            this.TabImage = Properties.Resources.DocumentCleanup;
        }

        protected override void IniUi()
        {
            listType.Items.Clear();
            foreach(string exe in global.BatchRename.Extensions)
            {
                listType.Items.Add(exe);
            }

            listCharacter.Items.Clear();
            foreach (string c in global.BatchRename.IncludeCharacters)
            {
                listCharacter.Items.Add(c);
            }
        }

        protected override void ApplyButton()
        {
            //List<string> listBoxItems = new List<string>();
            //for (int i = 0; i < listCharacter.Items.Count; i++)
            //{
            //    string listBoxItem = listCharacter.Items[i].ToString();
            //    listBoxItems.Add(listBoxItem);
            //}
            //List<string> extensions = new List<string>();
            //for (int i = 0; i < listType.Items.Count; i++)
            //{
            //    string item = listType.Items[i].ToString();
            //    string[] exeArray = item.Split(';');
            //    foreach (string extension in exeArray)
            //        extensions.Add(extension);
            //}
            //if (extensions.Count == 0)
            //    return;
            //global.BatchRename.Extensions = extensions;
            //global.BatchRename.IncludeCharacters = listBoxItems;

            //DirectoryHelper.TraverseAndRemove(RootPath, extensions, listBoxItems);

            if (global.BatchRename.Extensions.Count == 0)
                return;
            DirectoryHelper.TraverseAndRemove(RootPath, global.BatchRename.Extensions, global.BatchRename.IncludeCharacters);
            DirectoryHelper.DeleteEmptyFolders(RootPath);
            global.SaveHistory();
            SendMsg("清理完成", LogLevel.INFO);
        }

        private void UpdataList()
        {
            List<string> listBoxItems = new List<string>();
            for (int i = 0; i < listCharacter.Items.Count; i++)
            {
                string listBoxItem = listCharacter.Items[i].ToString();
                listBoxItems.Add(listBoxItem);
            }
            List<string> extensions = new List<string>();
            for (int i = 0; i < listType.Items.Count; i++)
            {
                string item = listType.Items[i].ToString();
                string[] exeArray = item.Split(';');
                foreach (string extension in exeArray)
                    extensions.Add(extension);
            }
            if (extensions.Count == 0)
                return;
            global.BatchRename.Extensions = extensions;
            global.BatchRename.IncludeCharacters = listBoxItems;
            global.SaveHistory();
        }


        private void btnAddType_Click(object sender, EventArgs e)
        {
            AddExtensions frm = new AddExtensions(global);
            if(frm.ShowDialog() == DialogResult.OK)
            {
                listType.Items.Clear();
                foreach(string item in global.BatchRename.Extensions)
                {
                    listType.Items.Add(item);
                }
                //保存修改参数到文件
                //List<string> listBoxItems = new List<string>();
                //for (int i = 0; i < listCharacter.Items.Count; i++)
                //{
                //    string listBoxItem = listCharacter.Items[i].ToString();
                //    listBoxItems.Add(listBoxItem);
                //}
                List<string> extensions = new List<string>();
                for (int i = 0; i < listType.Items.Count; i++)
                {
                    string item = listType.Items[i].ToString();
                    string[] exeArray = item.Split(';');
                    foreach (string extension in exeArray)
                        extensions.Add(extension);
                }
                if (extensions.Count == 0)
                    return;
                //global.BatchRename.Extensions = extensions;
                //global.BatchRename.IncludeCharacters = listBoxItems;
                //global.SaveHistory();
                UpdataList();
            }
        }

        private void btnDelType_Click(object sender, EventArgs e)
        {
            if (listType.Items.Count == 0)
                return;
            if (listType.SelectedItem != null)
            {
                listType.Items.Remove(listType.SelectedItem);
            }
            else
            {
                listType.Items.RemoveAt(listType.Items.Count - 1);
            }
            UpdataList();

        }

        private void btnClrType_Click(object sender, EventArgs e)
        {
            if (listType.Items.Count == 0)
                return;
            listType.Items.Clear();
            UpdataList();
        }

        private void btnAddCharacter_Click(object sender, EventArgs e)
        {
            bool foundMatch = false;
            string str = tbxCharacter.Text;
            if (str == "")
            {
                return;
            }
            else
            {
                for (int i = 0; i < listCharacter.Items.Count; i++)
                {
                    string listBoxItem = listCharacter.Items[i].ToString();

                    if (listBoxItem.Equals(str))
                    {
                        foundMatch = true;
                        break;
                    }
                }
                if (!foundMatch)
                {
                    listCharacter.Items.Add(str);
                    //List<string> listBoxItems = new List<string>();
                    //for (int i = 0; i < listCharacter.Items.Count; i++)
                    //{
                    //    string listBoxItem = listCharacter.Items[i].ToString();
                    //    listBoxItems.Add(listBoxItem);
                    //}
                    //global.BatchRename.IncludeCharacters = listBoxItems;
                    //global.SaveHistory();
                    UpdataList();
                }

            }
        }

        private void btnDelCharacter_Click(object sender, EventArgs e)
        {
            if (listCharacter.Items.Count == 0)
                return;
            if (listCharacter.SelectedItem != null)
            {
                listCharacter.Items.Remove(listCharacter.SelectedItem);
            }
            else
            {
                listCharacter.Items.RemoveAt(listCharacter.Items.Count - 1);
            }
            UpdataList();
        }

        private void btnClrCharacter_Click(object sender, EventArgs e)
        {
            if (listCharacter.Items.Count == 0)
                return;
            listCharacter.Items.Clear();
            UpdataList();
        }

        private void rsCopy_Click(object sender, EventArgs e)
        {
            if (listType.Items.Count == 0 && listCharacter.Items.Count == 0)
                return;
            else if (listType.SelectedItem != null)
            {
                Clipboard.SetText(listType.SelectedItem.ToString());
            }
            else if (listCharacter.SelectedItem != null)
            {
                Clipboard.SetText(listCharacter.SelectedItem.ToString());
            }
        }
    }
}
